package com.funtonow.jqdk.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.adapter.LoanListAdapter;
import com.funtonow.jqdk.base.BaseActivity;
import com.funtonow.jqdk.base.Constants;
import com.funtonow.jqdk.base.SqjAppliaction;
import com.funtonow.jqdk.base.UrlUtils;
import com.funtonow.jqdk.bean.GameThirdInfoListBean;
import com.funtonow.jqdk.bean.LoanInfoBean;
import com.funtonow.jqdk.bean.UserInfoBean;
import com.funtonow.jqdk.utils.ActivityManager;
import com.funtonow.jqdk.utils.HttpUtil;
import com.funtonow.jqdk.utils.SPUtil;
import com.funtonow.jqdk.utils.StatisticsUtils;
import com.funtonow.jqdk.utils.ToastUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

import static com.funtonow.jqdk.base.UrlUtils.TUI_JIAN;

/**
 * @author Jiajien
 * @date 2019/6/26
 * 浮层1
 */
public class FloatingLayerActivity2 extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.rlv_recommend)
    RecyclerView rlvRecommend;
    @BindView(R.id.ll_float)
    LinearLayout llFloat;

    private LoanListAdapter recommendAdapter;
    private List<GameThirdInfoListBean> gameThirdInfoListBeans = new ArrayList<>();


    @Override
    public int getLayoutRes() {
        return R.layout.floating_layer_layout2;
    }

    @Override
    public void onCreateActivity(@Nullable Bundle savedInstanceState) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(lp);
        initRecyclerView(rlvRecommend);
        getAppId();
        recommendAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (SPUtil.getUserInfo() != null && recommendAdapter.getItem(position).getVipfw() == 1
                        && SPUtil.getUserInfo().getHasVip() == 0) {
                    ToastUtils.show("该产品仅限会员申请");
                    startActivity(new Intent(FloatingLayerActivity2.this, VipActivity.class));
                } else {
                    GameThirdInfoListBean gameThirdInfoListBean = recommendAdapter.getItem(position);
                    assert gameThirdInfoListBean != null;
                    StatisticsUtils.clickUrl(gameThirdInfoListBean);
                    StatisticsUtils.intentWebView(FloatingLayerActivity2.this,
                            gameThirdInfoListBean.getUrl(),gameThirdInfoListBean.getName(),
                            gameThirdInfoListBean.getChanpinid());
                }
                finish();
            }
        });
    }


    private void initRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recommendAdapter = new LoanListAdapter();
        recyclerView.setAdapter(recommendAdapter);
    }

    @OnClick({R.id.ib_close})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_close:
                finish();
                ActivityManager.getInstance().killActivity(WebViewActivity.class);
                break;
            default:
                break;
        }
    }


    /**
     * 推荐列表
     */
    private void recommend() {
        UserInfoBean userInfoBean = SPUtil.getUserInfo();
        Map<String, String> param = new HashMap<>();
        assert userInfoBean != null;
        param.put("appid", Constants.OWN_APP_ID);
        param.put("token", userInfoBean.getToken());
        param.put("openid", userInfoBean.getOpenid());
        param.put("showappid", Constants.DATA_APP_ID);
        HttpUtil.getInstance().postDataAsynToNet(TUI_JIAN, param, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {

                JSONObject jsonObject = JSON.parseObject(SqjAppliaction.loanInfoBean.getCpMap());
                String jsonArray = JSON.parseObject(jsonString).getJSONArray("data").toJSONString();
                List<String> stringList = JSON.parseArray(jsonArray, String.class);
                for (String s : stringList) {
                    for (LoanInfoBean.HeziInfoListBean heziInfoListBean : SqjAppliaction.loanInfoBean.getHeziInfoList()) {
                        if (s.equals(heziInfoListBean.getGdid())) {
                            for (LoanInfoBean.GdlistBean gdlistBean : SqjAppliaction.loanInfoBean.getGdlist()) {
                                if (s.equals(gdlistBean.getGdid())) {
                                    GameThirdInfoListBean gameThirdInfoListBean = JSON.parseObject(jsonObject.getJSONObject(gdlistBean.getChanpinid()).toJSONString(), GameThirdInfoListBean.class);
                                    gameThirdInfoListBean.setVipfw(gdlistBean.getVipfw());
                                    gameThirdInfoListBean.setUrl(gdlistBean.getUrl());
                                    gameThirdInfoListBean.setHeziId(heziInfoListBean.getType());
                                    gameThirdInfoListBean.setIndex(heziInfoListBean.getInde());
                                    gameThirdInfoListBean.setGdId(s);
                                    gameThirdInfoListBeans.add(gameThirdInfoListBean);
                                }
                            }
                        }
                    }
                }
                if (gameThirdInfoListBeans.size() > 0) {
                    llFloat.setVisibility(View.VISIBLE);
                    recommendAdapter.setNewData(gameThirdInfoListBeans.subList(0, 1));
                } else {
                    finish();
                    ActivityManager.getInstance().killActivity(WebViewActivity.class);
                }

                StatisticsUtils.baoGuang(Constants.HEZI_ID_25);

            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }

    /**
     * 获取数据appId
     */
    private void getAppId() {
        HttpUtil.getInstance().getDataAsynFromNet(UrlUtils.GET_APPID, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                Constants.DATA_APP_ID = JSON.parseObject(jsonString).getString("appId");
                recommend();
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }

}
